package com.ygqh.baby.controller.open;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.controller.mobile.BaseController;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.utils.DateConvertUtils;
import com.ygqh.baby.utils.ImageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 开放活动接口（无需登录）
 *
 * @author: sunshuo
 * @date: 2019/1/24 12:05
 */
@RequestMapping("/open/action")
@RestController
public class OpenActionController extends BaseController {

	@Autowired
	private RedisDao redisDao;
	@Autowired
	private FileService fileService;

	@Value("${image.domain}")
	private String domain;
	@Value("${image.upload.temp.url}")
	private String tempDir;

	/**
	 * 情人节活动
	 *
	 * @param callback     callback
	 * @param source       活动source
	 * @param babyName     宝宝名字
	 * @param babyBirthDay 宝宝生日
	 * @param yourName     你的名字
	 * @param yourBirthDay 你的生日
	 * @return
	 */
	@RequestMapping("actionByQingRen")
	public JSONPObject getResultByQingRen(@RequestParam String callback, @RequestParam String source,
										  String babyName, Date babyBirthDay,
										  String yourName, Date yourBirthDay) throws Exception {
		Message resultMsg;
		if (StringUtils.isBlank(babyName)) {
			resultMsg = Message.error("请输入宝宝名字", null);
		} else if (babyBirthDay == null) {
			resultMsg = Message.error("请输入宝宝阳历生日", null);
		} else if (StringUtils.isBlank(yourName)) {
			resultMsg = Message.error("请输入你的名字", null);
		} else if (yourBirthDay == null) {
			resultMsg = Message.error("请输入你的阳历生日", null);
		} else {
			String key = source + "_" + babyName + "_" + DateConvertUtils.format(babyBirthDay)
					+ "_" + yourName + "_" + DateConvertUtils.format(yourBirthDay);
			Map<String, Object> result = getQingrenResultFromRedis(key, yourName, babyName);
			resultMsg = Message.success(result);
		}
		return new JSONPObject(callback, resultMsg);
	}

	@SuppressWarnings({"unchecked"})
	private Map<String, Object> getQingrenResultFromRedis(String key, String yourName, String babyName) throws Exception {
		String result = redisDao.get(key);
		if (StringUtils.isNotBlank(result)) {
			return JSONObject.parseObject(result, Map.class);
		}
		Map<String, Object> data = new HashMap<>(2);
		// 生成随机结果
		int random = (int) (Math.random() * 8 + 1);
		data.put("result", random);

		/*
		 * 生成指定分享图
		 */
		String fileName = "qingren_ditu_2019_" + random + ".jpg";
		// 底图阿里云路径
		String sourceUrl = domain + "default/" + fileName;
		// 本地路径
		String localPath = tempDir + "/" + fileName;
		File localFile = ImageUtils.getFile(localPath);
		if (!localFile.exists()) {
			ImageUtils.saveImageToDisk(sourceUrl, tempDir, fileName);
		}
		String filePathAfterPress = tempDir + "/" + key + ".jpg";

		String text = "@" + yourName + "和" + babyName;
		int x = 455 - (text.length() - 7) * 30;
		ImageUtils.pressText(text, localPath, filePathAfterPress,
				"微软雅黑", 20, new Color(218, 89, 63), 30, x, 312, 1.0f);
		File file = new File(filePathAfterPress);
		String codeUrl = fileService.uploadImage(file, FileType.QrCode, true);
		data.put("imageUrl", codeUrl);
		file.delete();

		redisDao.set(key, JSONObject.toJSONString(data), DateConvertUtils.addYear(new Date(), 1).getTime());
		return data;
	}

}
